var AllArticlesPage = "http://go.zoominfo.com/LP=1204";
var IndexPage = "http://go.zoominfo.com/LP=1207";
var MenuDarkBg = "https://zoom-insights.googlecode.com/svn/images/menu_dark_bg.png";
var MenuNormalBg= "https://zoom-insights.googlecode.com/svn/images/menu_normal_bg.png";
var SubsNewsletter = "http://go.zoominfo.com/LP=1205";
var IndexXML = "http://img.en25.com/Web/ZoomInformation/{f515f442-7926-4cfe-9bb0-3ecbe00b591a}_index.xml";
var IndexRenderXML = "http://img.en25.com/Web/ZoomInformation/{c7b2da4a-0d05-4d87-9ca6-b7ef5683310d}_indexRender.xml";
document.domain = "go.zoominfo.com";


	
	
/////////////helpers start //////////////////
	


	
	
	function getParameterByName(name)
	{
	  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	  var regexS = "[\\?&]" + name + "=([^&#]*)";
	  var regex = new RegExp(regexS);
	  var results = regex.exec(window.location.search);
	  if(results == null)
		return "";
	  else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}	
	/////////////helpers start //////////////////
	
	/////////////window load and events start //////////////////
	
	
	
	$(window).load(function(){
		//alert("2");	
		if($("#pagetype").val() == "sub"){
			if(window.top.location.href == window.location)
				window.location = IndexPage + "?page=" + encodeURI(window.location) + "&category=" + document.getElementById("pagecategory").value;
			else{
				/*
				lets resize the iframe shall we?	
					we are assuming that the parent page has an element with 
					id 'contentframe' which is the iframe we will resize
				*/				
				
				if(document.getElementById("pageid") != null && document.getElementById("pageid").value == "articles")
				{
					window.parent.document.getElementById("SideBar2").style.display="none";
					window.parent.document.getElementById("SideBar1").style.display="block";
					window.parent.document.getElementById("AboutUs").style.display="block";
					
					if(getParameterByName("category")!=""){
						window.parent.document.getElementById("SideBar2").style.display="block";
						window.parent.document.getElementById("SideBar1").style.display="none";
						window.parent.document.getElementById("AboutUs").style.display="none";
					}
				}
				else
				{
					window.parent.document.getElementById("SideBar2").style.display="block";
					window.parent.document.getElementById("AboutUs").style.display="none";
					window.parent.document.getElementById("SideBar1").style.display="none";
				}
					
				window.parent.document.getElementById("contentframe").style.width = "300px";
				window.parent.document.getElementById("contentframe").style.height = "300px";
				
				var w = $(document).width();
				var h = $(document).height();
				
				window.parent.document.getElementById("contentframe").style.width = w + "px";
				window.parent.document.getElementById("contentframe").style.height = h + "px";
				
				window.parent.document.getElementById("pagefooter").style.top = "0px";
				window.parent.document.getElementById("pagefooter").style.top = 
					($(window.parent.document).outerHeight() + 10 ) + "px";

				
				
				try{
				//alert(document.getElementById("pagecategory").value + " : " + mainMenu);
				window.parent.setupMenu(document.getElementById("pagecategory").value , mainMenu, false);
				}catch(ex1){}
			}
		}else{
			if(getParameterByName("page")!=""){
				document.getElementById("contentframe").src = decodeURI(getParameterByName("page"));

				if(getParameterByName("category")!=""){					
					mainMenuCurrentItem = getParameterByName("category");

				}
			}
		}
	});
	
	$(window).load(function(){
		CenterAlignItems();
		try
		{
			setupMenu(mainMenuCurrentItem, mainMenu, false);
		}catch(EXP){alert(EXP);}
		
		
	});
	
	$(window).resize(function() {
  		CenterAlignItems();
	});
	
	function CenterAlignItems(){
		var w = $(document).width();
  		var h = $(document).height();		
		
		var items = centerAlignTheseItems.split(",");
		
		for(i = 0;i<items.length;i++){
			var obj = $("#" + items[i]);
			if(obj!=null){
				var left = (w/2) - (obj.outerWidth()/2);
				obj.css({"left": left+"px"}); 
			}
		}	
		
		//position main drop down menu over the banner
		var top = $("#MainBanner").position().top + $("#MainBanner").outerHeight() - 15;
		var left = $("#MainBanner").position().left + 15;
		
		$("#MainMenuDiv").css({"top": top+"px", "left": left+"px"});
		
		//position the down arrow over the main drop down menu
		top = $("#MainMenuDiv").position().top+ 15;
		left = $("#MainMenuTable").outerWidth() - $("#MainMenuTable").position().left - ($("#MenuArrow").outerWidth()/2);

		$("#MenuArrow").css({"top": top+"px", "left": left+"px"});
		
		//position main links menu over the banner
		top = $("#MainBanner").position().top + $("#MainBanner").outerHeight() - 15;
		left = $("#MainBanner").position().left + 25 + $("#MainMenuDiv").outerWidth();
		
		$("#LinksMenu").css({"top": top+"px", "left": left+"px"});
		
		//aligning SideBar2
		top = $("#sideBarPlaceHolder").position().top + 40;
		left = $("#MainHolderTable").position().left + $("#MainBanner").outerWidth() - $("#SideBar2").outerWidth();
		$("#SideBar2").css({"top": top+"px", "left": left+"px"});

		$("#SideBar1").css({"top": top+"px", "left": left+"px"});

		//$("#pagefooter").css({"top": ($(document).outerHeight() + 10 )+"px"});
		
	
	}
	
	
	
	var centerAlignTheseItems = "MainHolderTable,pagefooter";
	
	/////////////window load and events end //////////////////
	
	
	/////////////main menu drop down functions start //////////////////
	var mainMenu = "M,R,S,ChooseMarket";
	var mainMenuCurrentItem = "ChooseMarket";
	var menuArrowDown = 1;
	
	function openContent(category, subcategory){
		//alert(category + " : " + subcategory);
		if(mainMenuCurrentItem == "ChooseMarket"){
			return;
		}
		mainMenuCurrentItem = category;
		if(subcategory == "")
		document.getElementById("contentframe").src = AllArticlesPage + "?category=" + mainMenuCurrentItem;
		else
		document.getElementById("contentframe").src = AllArticlesPage + "?category=" + mainMenuCurrentItem + "&subcategory=" + mainMenuCurrentItem + "_" + subcategory;
	}
	
	function setupMenu(currentSelection, menuItemsStr, invokeFuncs){
		showMeHideOthers(currentSelection, menuItemsStr, invokeFuncs);
	}
	
	function displayMarketMenuOptions(menuItemsStr){
		var menuItems = menuItemsStr.split(",");
		for(i=0;i<menuItems.length;i++){
			document.getElementById(menuItems[i]).setAttribute("class", "visibleRow");
		}
	}
	
	function showMeHideOthers(id, menuItemsStr, arrowId, invokeFuncs){
		document.getElementById(id).setAttribute("class", "visibleRow");
		var menuItems = menuItemsStr.split(",");
		var count = 0;
		for(i=0;i<menuItems.length;i++){
			if(id!=menuItems[i])
				{					
					if(document.getElementById(menuItems[i]).getAttribute("class")!= "hiddenRow"){
						document.getElementById(menuItems[i]).setAttribute("class", "hiddenRow");
						count++;
					}
				}
		}
		
		if(count==0)
			arrowClicked(arrowId, false);
			
		mainMenuCurrentItem = id;
		if(invokeFuncs!=false)
		{
			arrowClicked(arrowId, false);
			return;
		}
		
	}
	
	function arrowClicked(arrowId, invokeFuncs){
		var obj = document.getElementById(arrowId);
		
		
		
		if(menuArrowDown == 1){
			if(invokeFuncs)
				displayMarketMenuOptions(mainMenu);		
			//try{obj.src= obj.getAttribute('upImg');}catch(exp){}			
			menuArrowDown = 0;

		}else{
			try{
				if(invokeFuncs)
					showMeHideOthers(mainMenuCurrentItem, mainMenu, false);
			}catch(exception){		}		
			//try{obj.src= obj.getAttribute('downImg');}catch(exp){}					
			menuArrowDown = 1;
		}
	}
	
	function hoverHighlight(obj, styleName){
		if(menuArrowDown != 1){
			obj.setAttribute('class',styleName);
		}
	}
	
	function changeBG(obj, bgType){
		if(bgType == "dark")
			img = MenuDarkBg;
		else
			img = MenuNormalBg;
		obj.style.backgroundImage = "url(" + img + ")";
		obj.style.backgroundRepeat= "repeat";
	}
	
	/////////////main menu drop down functions end //////////////////
	
	
	function Addlog(obj)	{
		$("#log").append("<br/>" + obj);
	}
	
	
	$('#log').click(function() {
  		alert('Handler for .blur() called.');
	});
	

	/////////////XML functions start //////////////////
		
	//area XML parsing
	function loadXML(fileName)
	{	
		if (window.XMLHttpRequest)
	  	{// code for IE7+, Firefox, Chrome, Opera, Safari
	  		
			xmlhttp=new XMLHttpRequest();
			//alert(xmlhttp); 
	  	}			
		else
	  	{// code for IE6, IE5
	  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  	}
		
		//xmlhttp = getXMLHTTPObject();
		xmlhttp.open("GET",fileName,false);
		
		xmlhttp.send();
		//alert("p");
		xmlDoc=xmlhttp.responseXML;
		return xmlDoc;
	}
	
	var xml="";
	var xsl="";
	
	function loadXmls(){
		xml=loadXML(IndexXML);
		alert(IndexXML + " : " + IndexRenderXML + " : " + xml);
		xsl=loadXML(IndexRenderXML);
	}
	
	function displayResult()
{// code loadXML IE
if (window.ActiveXObject)
  {
  ex=xml.transformNode(xsl);
  
  document.getElementById("content").innerHTML=ex;
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {

  xsltProcessor=new XSLTProcessor();
  xsltProcessor.importStylesheet(xsl);
  resultDocument = xsltProcessor.transformToFragment(xml,document);

 document.getElementById("content").appendChild(resultDocument);

  }
}

	/////////////XML functions end //////////////////

function selectCategory(categoryName)
{
	var elem = document.getElementsByTagName("div");

        for(var i = 0; i < elem.length; i++)
        {
			try{
				//alert(elem[i].id + " : " + categoryName);
				if(elem[i].getAttribute("type") == "category" && elem[i].id != categoryName)
				{
					$("#" + elem[i].id).hide();
				}
				if(elem[i].getAttribute("type") == "category" && elem[i].id == categoryName)
				{
					document.getElementById(elem[i].id + "__TITLE").className = "articleTitle";
				}
				
			}catch(ex){alert(ex);}
		}
}

function selectCategories(categoryName, subCategoryName)
{
		var elem = document.getElementsByTagName("div");
		//alert(categoryName + " : " +  subCategoryName);
		if(categoryName!="")
		{
			selectCategory(categoryName);
			try{
			window.parent.setupMenu(categoryName, mainMenu, false);
			}catch(ex1){alert(ex1);};
			if(subCategoryName!="")
			{				
				for(var i = 0; i < elem.length; i++)
				{
					try{
						//alert(elem[i].id + " : " + categoryName);
						if(elem[i].getAttribute("type") == "subcategory" && elem[i].id != subCategoryName)
						{
							//alert(subCategoryName + " : " + elem[i].id);
							$("#" + elem[i].id).hide();
						}
						if(elem[i].getAttribute("type") == "subcategory" && elem[i].id == subCategoryName)
						{
							//$("#" + elem[i].id + "__SUBTITLE").toggleClass("articleTitle");
							document.getElementById(elem[i].id + "__SUBTITLE").className = "sideBarHeaderFont";
						}
					}catch(ex){alert(ex);}
				}
			}
		}
}

function showAllCategories(){
		var elem = document.getElementsByTagName("div");
		for(var i = 0; i < elem.length; i++)
        {
			try{
				//alert(elem[i].id + " : " + categoryName);
				if(elem[i].getAttribute("type") == "subcategory" ||  elem[i].getAttribute("type") == "category" )
				{
					$("#" + elem[i].id).show();
				}
			}catch(ex){alert(ex);}
		}
}

